package com.qf.compusdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.compusdemo.dao.UserDao;
import com.qf.compusdemo.entity.User;
import com.qf.compusdemo.entity.Wxs;
import com.qf.compusdemo.service.UserService;
import com.qf.compusdemo.vo.MessageVo;
import com.qf.compusdemo.vo.Ppage;
import com.qf.compusdemo.vo.tableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/regist")
public class registController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserDao userDao;

    @RequestMapping("/allusers")
    @ResponseBody
    public tableVo<User> test0(Ppage page, User user){

        Page<User> pp=new Page<>(page.getPage(),page.getLimit());
        IPage<User> ipage=userService.getAllUser(pp,user);
        tableVo<User> table=new tableVo<>();
        table.setCode("0");
        table.setMsg("ok");
        table.setData(ipage.getRecords());
        table.setCount(ipage.getTotal());
        System.out.println(table);
        return table;
    }

    @RequestMapping("/del")
    @ResponseBody
    public MessageVo test1(Integer[] ids){
        List lists=java.util.Arrays.asList(ids);
        int a=userService.delUser(lists);

        MessageVo message=new MessageVo();
        message.setStatus(a>0?true:false);
        message.setMessage(a>0?"success":"fail");
        return message;
    }

    @RequestMapping("/update")
    @ResponseBody
    public MessageVo test2(User uu){

        System.out.println(uu);
        int a=userService.updateUser(uu);

        MessageVo message=new MessageVo();
        message.setStatus(a>0?true:false);
        message.setMessage(a>0?"success":"fail");
        return message;

    }

    @RequestMapping("/adds")
    @ResponseBody
    public MessageVo test3(User uu){
//        System.out.println(uu);
        MessageVo message=new MessageVo();
        String username=uu.getUsername();
        String password=uu.getPassword();
        String auths=uu.getAuth();
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        if(userDao.selectCount(wrapper)>0){
            message.setStatus(false);
            message.setMessage("用户名已经存在");
            return message;
        }
        //normal,wx,admin
        switch (auths) {
            case "0":
                auths = "normal";
                break;
            case "1":
                auths = "normal,wx,dian";
                break;
            case "2":
                auths = "normal,wx,mu";
                break;
            case "3":
                auths = "normal,wx,shui";
                break;
            case "4":
                auths = "normal,adminst";
                break;
            case "5":
                auths = "normal,adminss";
                break;
            case "6":
                auths = "normal,adminjxl";
                break;
            case "7":
                auths = "normal,admino";
                break;
        }

        User user=new User(null,username,password,auths);

        int a=userService.adds(user);

        message.setStatus(a>0?true:false);
        message.setMessage(a>0?"success":"fail");
        return message;


    }











}
